#pragma once

namespace xyx
{
	template<class K>
	struct BSTNode
	{
	
	BSTNode(const K& val)
		:_left(nullptr)
		,_right(nullptr)
		,_key(val)
	{}



	BSTNode* _left;
	BSTNode* _right;
	K _key;
	};
	
	template<class K>
	class BSTree
	{
	public:
		typedef BSTNode<K> Node;
		BSTree()
			:_root(nullptr)
		{}

		BSTree(const BSTree<K>& t)
		{
			_root = Copy(t._root);
		}

		BSTree<K>& operator=(BSTree<K> t)
		{
			swap(_root, t._root);
			return *this;
		}

		~BSTree()
		{
			Destroy(_root);
		}




		bool Insert(const K& key)
		{
			//_root	为空直接插入
			if (_root == nullptr)
			{
				_root = new Node(key);
				return true;
			}
			else //找到合适位置插入
			{
				//遍历一下树，若key存在返回false
				//不存在这个key则插入数据
				//parent用于保存父亲节点
				Node* parent = nullptr;
				Node* cur = _root;
				while (cur)
				{
					//主逻辑
					if (cur->_key < key)
					{
						parent = cur;
						cur = cur->_right;
					}
					else if (cur->_key > key)
					{
						parent = cur;
						cur = cur->_left;
					}
					else
					{
						return false;
					}
				}
				cur = new Node(key);
				//任然需要判断连接的位置
				if (parent->_key > key)
				{
					parent->_left = cur;
				}
				else
				{
					parent->_right = cur;
				}
				return true;
			}
		}

		void InOrder()
		{
			_inOrder(_root);
			cout << endl;
		}

		bool Find(const K& key)
		{			
			Node* cur = _root;
			while (cur)
			{
				//比cur大去右边找
				//比cur小去做边找
				if (cur->_key > key)
					cur = cur->_left;
				else if (cur->_key < key)
					cur = cur->_right;
				else//找到了
					return true;
			}

			//此时cur为空，即没找到
			return false;
		}

		bool Erase(const K& key)
		{
			Node* parent = nullptr;
			Node* cur = _root;

			while (cur)
			{
				//比cur的值小
				if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				//比cur的值大
				else if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else //找到了 
				{
					//大体分两种情况，1、托孤 2、找子树的最大值交换
					//左边为空的情况
					if (cur->_left == nullptr)
					{
						//边界情况
						if (cur == _root)
						{
							_root = cur->_right;
						}
						else
						{
							//需要考虑cur的位置
							if (parent->_left == cur)
							{
								parent->_left = cur->_right;
							}
							else
							{
								parent->_right = cur->_right;
							}
						}
					}
					else if (cur->_right == nullptr)//右为空
					{
						//边界情况
						if (cur == _root)
						{
							_root = cur->_left;
						}
						else
						{
							//需要考虑cur的位置
							if (parent->_left == cur)
							{
								parent->_left = cur->_left;
							}
							else
							{
								parent->_right = cur->_left;
							}
						}
					}
					else//左右不为空，与左子树的最大值交换
					{
						//这里parent不能为null，否则下面特殊情况会出错
						parent = cur;
						Node* leftMax = cur->_left;
						while (leftMax->_right)
						{
							parent = leftMax;
							leftMax = leftMax->_right;
						}

						swap(leftMax->_key, cur->_key);


						//特殊情况 _root->left == leftMax
						if (parent->_left == leftMax)
						{
							parent->_left = leftMax->_left;
						}
						else
						{
							parent->_right = leftMax->_left;
						}

						//符合统一删除逻辑
						cur = leftMax;
					}
					//统一删除逻辑
					delete cur;
					return true;
				}
			}
			return false;
		}

		bool FindR(const K& key)
		{
			return _FindR(_root, key);
		}

		bool InsertR(const K& key)
		{
			return _InsertR(_root, key);
		}

		bool EraseR(const K& key)
		{
			return _EraseR(_root, key);
		}

	private:
		void Destroy(Node*& root)
		{
			if (root == nullptr)
				return;

			Destroy(root->_left);
			Destroy(root->_right);
			delete root;
			root = nullptr;
		}

		Node* Copy(Node* root)
		{
			//递归出口
			if (root == nullptr)
				return nullptr;

			//前序遍历以此拷贝
			Node* newNode = new Node(root->_key);
			newNode->_left = Copy(root->_left);
			newNode->_right = Copy(root->_right);
			return newNode;
		}


		bool _EraseR(Node*& root, const K& key)
		{
			//key 不存在
			if (root == nullptr)
				return false;

			if (root->_key > key) //比root小，去root的左子树找
				return _EraseR(root->_left, key);
			else if(root->_key < key) //比root大，去root的左子树找
				return _EraseR(root->_right, key);
			else //找到了
			{
				// 1、左为空
				// 2、右为空
				// 3、左右不为空
				Node* del = root;//保存以便释放

				if (root->_left == nullptr)
				{
					root = root->_right;
				}
				else if (root->_right == nullptr)
				{
					root = root->_left;
				}
				else
				{
					Node* leftMax = root->_left;

					while (leftMax->_right)
					{
						leftMax = leftMax->_right;
					}
				
					swap(root->_key, leftMax->_key);

					return _EraseR(root->_left, key);
				}
				delete del;
				return true;
			}
		}

		bool _InsertR(Node*& root, const K& key)
		{
			if (root == nullptr)
			{
				root = new Node(key);
				return true;
			}

			if (root->_key > key)
			{
				return _InsertR(root->_left, key);
			}
			else if (root->_key < key)
			{
				return _InsertR(root->_right, key);
			}
			else
			{
				return false;
			}
		}

		bool _FindR(Node* root, const K& key)
		{
			if (root == nullptr)
				return false;

			if (root->_key > key)
			{
				return _FindR(root->_left, key);
			}
			else if (root->_key < key)
			{
				return _FindR(root->_right, key);
			}
			else
			{
				return true;
			}
		}

		void _inOrder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}

			_inOrder(root->_left);
			cout << root->_key << " ";
			_inOrder(root->_right);
		}

	private:
		Node* _root;
	};

	void testBST1()
	{
		int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
		BSTree<int> t;
		for (auto e : a)
		{
			t.Insert(e);
		}

		t.InOrder();

		//cout << endl;
		//cout << tree.find(7) << endl;
		//cout << tree.find(8) << endl;
		//cout << tree.find(14) << endl;
		//cout << tree.find(9) << endl;

		t.Erase(4);
		t.InOrder();

		t.Erase(6);
		t.InOrder();

		t.Erase(7);
		t.InOrder();

		t.Erase(3);
		t.InOrder();

		for (auto e : a)
		{
			t.Erase(e);
		}
		t.InOrder();
	}
	void testBST2()
	{
		int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
		BSTree<int> t;
		for (auto e : a)
		{
			t.InsertR(e);
		}

		t.InOrder();

		//cout << t.FindR(14);
		//cout << t.FindR(3);
		//cout << t.FindR(7);
		//cout << t.FindR(9);

		t.EraseR(4);
		t.InOrder();

		t.EraseR(6);
		t.InOrder();

		t.EraseR(7);
		t.InOrder();

		t.EraseR(3);
		t.InOrder();

		//for (auto e : a)
		//{
		//	t.EraseR(e);
		//}
		//t.InOrder();
	}

	void testBST3()
	{
		int a[] = { 8, 3, 1, 10, 6, 4, 7, 14, 13 };
		BSTree<int> t;
		for (auto e : a)
		{
			t.InsertR(e);
		}

		t.InOrder();

		BSTree<int> t1 = t;

		t1.InOrder();

	}
}



namespace xyx_kv
{
	template<class K, class V>
	struct BSTNode
	{

		BSTNode(const K& key, const V& value)
			:_left(nullptr)
			, _right(nullptr)
			, _key(key)
			, _value(value)
		{}



		BSTNode* _left;
		BSTNode* _right;
		K _key;
		V _value;
	};

	template<class K, class V>
	class BSTree
	{
	public:
		typedef BSTNode<K, V> Node;
		BSTree()
			:_root(nullptr)
		{}

		BSTree(const BSTree<K, V>& t)
		{
			_root = Copy(t._root);
		}

		BSTree<K, V>& operator=(BSTree<K, V> t)
		{
			swap(_root, t._root);
			return *this;
		}

		~BSTree()
		{
			Destroy(_root);
		}



		void InOrder()
		{
			_inOrder(_root);
			cout << endl;
		}

		Node* FindR(const K& key)
		{
			return _FindR(_root, key);
		}

		bool InsertR(const K& key, const V& val)
		{
			return _InsertR(_root, key, val);
		}

		bool EraseR(const K& key)
		{
			return _EraseR(_root, key);
		}

	private:
		void Destroy(Node*& root)
		{
			if (root == nullptr)
				return;

			Destroy(root->_left);
			Destroy(root->_right);
			delete root;
			root = nullptr;
		}

		Node* Copy(Node* root)
		{
			//递归出口
			if (root == nullptr)
				return nullptr;

			//前序遍历以此拷贝
			Node* newNode = new Node(root->_key, root->_value);
			newNode->_left = Copy(root->_left);
			newNode->_right = Copy(root->_right);
			return newNode;
		}


		bool _EraseR(Node*& root, const K& key)
		{
			//key 不存在
			if (root == nullptr)
				return false;

			if (root->_key > key) //比root小，去root的左子树找
				return _EraseR(root->_left, key);
			else if (root->_key < key) //比root大，去root的左子树找
				return _EraseR(root->_right, key);
			else //找到了
			{
				// 1、左为空
				// 2、右为空
				// 3、左右不为空
				Node* del = root;//保存以便释放

				if (root->_left == nullptr)
				{
					root = root->_right;
				}
				else if (root->_right == nullptr)
				{
					root = root->_left;
				}
				else
				{
					Node* leftMax = root->_left;

					while (leftMax->_right)
					{
						leftMax = leftMax->_right;
					}

					swap(root->_key, leftMax->_key);

					return _EraseR(root->_left, key);
				}
				delete del;
				return true;
			}
		}

		bool _InsertR(Node*& root, const K& key, const V& val)
		{
			if (root == nullptr)
			{
				root = new Node(key, val);
				return true;
			}

			if (root->_key > key)
			{
				return _InsertR(root->_left, key, val);
			}
			else if (root->_key < key)
			{
				return _InsertR(root->_right, key, val);
			}
			else
			{
				return false;
			}
		}

		Node* _FindR(Node* root, const K& key)
		{
			if (root == nullptr)
				return nullptr;

			if (root->_key > key)
			{
				return _FindR(root->_left, key);
			}
			else if (root->_key < key)
			{
				return _FindR(root->_right, key);
			}
			else
			{
				return root;
			}
		}

		void _inOrder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}

			_inOrder(root->_left);
			cout << root->_key << ":" << root->_value << endl;
			_inOrder(root->_right);
		}

	private:
		Node* _root;
	};

	void testKVtree1()
	{
		BSTree<string, string> dict;
		dict.InsertR("left", "左边");
		dict.InsertR("right", "右边");
		dict.InsertR("front", "前面");
		dict.InsertR("back", "后面");

		string str;
		while (cin >> str)
		{
			BSTNode<string, string>* ret = dict.FindR(str);
			if (ret)
			{
				cout << ret->_value << endl;
			}
			else
			{
				cout << "无此单词" << endl;
			}
		}
	}

	void TestBSTree2()
	{
		// 统计水果出现的次数
		string arr[] = { "西瓜", "苹果", "苹果", "西瓜", "苹果", "香蕉", "苹果", "香蕉" };
		BSTree<string, int> countTree;
		for (auto& str : arr)
		{
			auto ret = countTree.FindR(str);
			if (ret == nullptr)
			{
				countTree.InsertR(str, 1);
			}
			else
			{
				ret->_value++;
			}
		}

		countTree.InOrder();
	}
}